/**
 * 
 */
package com.palmwindActivity.app.main;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author albertma
 *
 */
public class ThreadPoolManager {
	public static final int  NET_WORK = 0;
	public static final int  SPLASH_SCREEN = 1;
	public static final int  BACKGROUND_RUNNING = 2;
	protected static final int END_SIZE = BACKGROUND_RUNNING + 1;

	public static int CORE_SIZE[] = {
		1,
		1,
		2,
	};
	
	public static int MAX_SIZE[] = {
		1,
		1,
		2,
	};
	
	public static int QUEUE_SIZE[] = {
		4,
		4,
		4,
	};
	
	protected static ThreadPoolExecutor threadpool[];
	
	protected static ThreadPoolManager instance;
	protected static Object mutex = new Object();
	private ThreadPoolManager()
	{
		threadpool = new ThreadPoolExecutor[END_SIZE];
	}
	
	public static ThreadPoolManager getInstance()
	{
		if(instance == null)
		{
			synchronized(mutex)
			{
				if(instance == null)
				{
					instance = new ThreadPoolManager();
				}
			}
			
		}
		return instance;
	}
	
	public synchronized ThreadPoolExecutor getThreadPool(int id)
	{
		if(id < 0 || id > threadpool.length)
		{
			return null;
		}
		if(ThreadPoolManager.threadpool[id] == null)
		{
			ThreadPoolManager.threadpool[id] = new ThreadPoolExecutor(CORE_SIZE[id], MAX_SIZE[id], 20000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3));
		}
		return ThreadPoolManager.threadpool[id];
	}
}
